package com.ysq.excavator.action;

import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.ysq.excavator.entity.FolderHtml;
import com.ysq.excavator.service.PublishService;
import com.ysq.excavator.util.CommonUtil;

/**
 * 转载验证相关
 * @author Administrator
 *
 */
@Controller("publishAction")
@Scope("prototype")
public class PublishAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	static Logger logger = CommonUtil.getLogger(PublishAction.class);
	
	@Resource(name = "publishService")
	protected PublishService publishService;
	
	private String publishUrl;
	private String publishDayRule;
	private String publishNameRule;
	
	private String firstPublish;
	private String regex;
	
	private List<FolderHtml> folderNameList;
	
	/** 表格页数 */
	private int page;
	/** 表格每页行数 */
	private int rows;
	
	/** 新增下载页面 */
	private String downloadUrl;
	private String htmlName;
	private String encoding;
	
	public String getPublishUrl() {
		return publishUrl;
	}
	public void setPublishUrl(String publishUrl) {
		this.publishUrl = publishUrl;
	}
	public String getPublishDayRule() {
		return publishDayRule;
	}
	public void setPublishDayRule(String publishDayRule) {
		this.publishDayRule = publishDayRule;
	}
	public String getPublishNameRule() {
		return publishNameRule;
	}
	public void setPublishNameRule(String publishNameRule) {
		this.publishNameRule = publishNameRule;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public List<FolderHtml> getFolderNameList() {
		return folderNameList;
	}
	public void setFolderNameList(List<FolderHtml> folderNameList) {
		this.folderNameList = folderNameList;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}
	public String getHtmlName() {
		return htmlName;
	}
	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}
	public String getEncoding() {
		return encoding;
	}
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	public String getFirstPublish() {
		return firstPublish;
	}
	public void setFirstPublish(String firstPublish) {
		this.firstPublish = firstPublish;
	}
	public String getRegex() {
		return regex;
	}
	public void setRegex(String regex) {
		this.regex = regex;
	}
	/**
	 * 获得发稿日期
	 */
	public void catchPublishDay(){
		System.out.println("get publishDay...");
		String publishDay = publishService.getPublishDay(publishUrl,publishDayRule);
		System.out.println(publishDay);
		CommonUtil.printResult(publishDay);
	}
	
	/**
	 * 获得转载网站名称
	 */
	public void catchPublishName(){
		System.out.println("get publishName...");
		String publishName = publishService.getPublishName(publishUrl,publishNameRule);
		if(publishName == null){
			publishName = "null";
		}
		CommonUtil.printResult(publishName);
	}
	
	/**
	 * 获得所有已下载的 html
	 * @return
	 */
	public void getPublishHtml(){
		
		int start = (page-1)*rows;
		int limit = rows;
		
		String dataJson = publishService.getDownloadHtmlJson(start,limit);
		
		CommonUtil.printResult(dataJson);
	}
	
	/**
	 * 删除下载的 html
	 */
	public void deleteDownloadHtml(){
		logger.info("delete html...");
		
		String result = publishService.deleteDownloadHtml(folderNameList);
		
		CommonUtil.printResult(result);
	}
	
	/**
	 * 下载新的页面
	 */
	public void downloadNewHtml(){
		logger.info("download new html...");
		String result = publishService.downloadNewHtml(downloadUrl,htmlName,encoding);
		CommonUtil.printResult(result);
	}
	
	/**
	 * 通过正则进行二次过滤
	 */
	public void getRegexMatch(){
		logger.info("getRegexMastch...");
		
		String result = publishService.getRegexMatch(firstPublish, regex);
		CommonUtil.printResult(result);
	}
}
